<template>
  <div>
    <el-form
      ref="loginFormRef"
      :model="ruleForm"
      :rules="rules"
      class="demo-ruleForm"
      size="large"
    >
      <el-form-item prop="username">
        <el-input
          v-model="ruleForm.iphone"
          :placeholder="loginData.iphone"
        >
          <template #prefix>
            <el-icon class="el-input__icon"><user /></el-icon>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input v-model="ruleForm.password" type="password" :placeholder="loginData.password">
          <template #prefix>
            <el-icon class="el-input__icon"><lock /></el-icon>
          </template>
        </el-input>
      </el-form-item>
    </el-form>

    <div class="login-btn">
      <el-button
        :icon="CircleClose"
        round
        @click="resetForm(loginFormRef)"
        size="large"
        >重置</el-button
      >
      <el-button
        :icon="UserFilled"
        round
        @click="login(loginFormRef)"
        size="large"
        type="primary"
        :loading="loading"
      >
        登录
      </el-button>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive, toRaw, computed } from "vue";
import { CircleClose, UserFilled } from "@element-plus/icons-vue";
import { mainStore } from "@/stores";
import { ElNotification } from "element-plus";
import { getTimeState } from "@/utils/util";
import {useRouter} from "vue-router"

let mStore = mainStore();
let router = useRouter()
const ruleForm = reactive({
  iphone: "",
  password: "",
});

const rules = reactive({
  iphone: [{ required: true, message: "请输入您的电话号码", trigger: "blur" }],
  password: [{ required: true, message: "请输入您的密码", trigger: "blur" }],
});

// 登录信息的提示
let loginData = computed(()=>{
    return mStore.loginData
})

let loginFormRef = ref();

const loading = ref(false);

let timer = ref(null);

// resetForm注册按钮
const resetForm = (formEl) => {
  if (!formEl) return;
  formEl.resetFields();
};

// 登录按钮
let login = (formEl) => {
  if (!formEl) return;
  formEl.validate( (valid) => {
    if (!valid) return;
    loading.value = true;
    if (!timer.value) {
      timer.value = setTimeout(async () => {
        try {
          let data = toRaw(ruleForm);
          let res = await mStore.login(data);
          console.log(res,"后台返回来的数据");
          if (res == "ok") {
            router.replace({
                name:"home"
            })
            ElNotification({
              title: getTimeState(),
              message: "欢迎登录 后台管理系统",
              type: "success",
              duration: 3000,
            });
          } else {
            ElNotification({
              title: getTimeState(),
              message: res,
              type: "error",
              duration: 3000,
            });
          }
        } finally {
          loading.value = false;
          timer.value = null;
          ruleForm.iphone = ""
          ruleForm.password = ""
        }
      }, 1000);
    }
  });
};
</script>

<style lang="scss" scoped>
@import "../index.scss";
</style>